#include "gofgame/dungeonkeeper/ui/ProduceElementView.h"
#include "gofgame/dungeonkeeper/ui/ProduceUpgradeEventArgs.h"
#include "gofgame/dungeonkeeper/ui/MessageViewDialog.h"
#include "gofgame/common/ui/TableLayout.h"
#include "gofgame/common/ui/TableLayoutData.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"
#include "gofgame/common/graphics/HornedTextureBrush.h"

using namespace gofgame::dungeonkeeper::ui;

HornedTextureBrushResource ProduceElementView::elementBrushResource(20, 20, "gofgame/dungeonkeeper/ui/element.png");

bool ProduceElementView::init() {
	if (!Container::init()) {
		return false;
	}

	this->setBackgroundBrush(elementBrushResource.unwrap());
	this->setPadding(20);
	TableLayout *tableLayout = new TableLayout(30);
	this->setHeight(
		tableLayout->getMinRowHeight() * 2 + 
		this->getPaddingTop() + 
		this->getPaddingBottom());

	tableLayout->setCellPadding(2);
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(2));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::absolute(150));
	tableLayout->setCellPadding(2);
	this->setLayout(tableLayout);
	TableLayoutData *tableLayoutData;

	this->image.initialize();
	this->image->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(2);
	this->image->setLayoutData(tableLayoutData);
	this->addChild(this->image);

	Ref<Label> nameDescLabel;
	nameDescLabel.initialize();
	nameDescLabel->init();
	nameDescLabel->setText("Name: ");
	nameDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(nameDescLabel);
	this->nameLabel.initialize();
	this->nameLabel->init();
	this->nameLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->nameLabel);

	Ref<Label> levelDescLabel;
	levelDescLabel.initialize();
	levelDescLabel->init();
	levelDescLabel->setText("Level: ");
	levelDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(levelDescLabel);
	this->levelLabel.initialize();
	this->levelLabel->init();
	this->levelLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->levelLabel);

	this->upgradeButton.initialize();
	this->upgradeButton->init();
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(2);
	this->upgradeButton->setLayoutData(tableLayoutData);
	this->upgradeButton->setBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			Brush::white()
		)
	);
	this->upgradeButton->setPressedBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			new LinearGradientBrush(0, 0, 0, 0, 1, 0, 0, 1)
		)
	);
	this->addChild(this->upgradeButton);

	Ref<Label> descriptionDescLabel;
	descriptionDescLabel.initialize();
	descriptionDescLabel->init();
	descriptionDescLabel->setText("Description: ");
	descriptionDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(descriptionDescLabel);
	this->descriptionLabel.initialize();
	this->descriptionLabel->init();
	this->descriptionLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setColSpan(3);
	this->descriptionLabel->setLayoutData(tableLayoutData);
	this->addChild(this->descriptionLabel);

	this->upgradeButton->click() += event_handler<ComponentEventArgs>(
		this, &ProduceElementView::upgradeButton_click);

	return true;
}

ProduceElementViewInfo ProduceElementView::getProduceElementViewInfo() {
	ProduceElementViewInfo pevi;
	pevi.imageResourceName = this->image->getResourceName();
	pevi.name = this->nameLabel->getText();
	pevi.description = this->descriptionLabel->getText();
	sscanf(this->levelLabel->getText(), "%d", &pevi.level);
	return pevi;
}

void ProduceElementView::setProduceElementViewInfo(const ProduceElementViewInfo &pevi) {
	char text[32];
	this->image->setResourceName(pevi.imageResourceName.c_str());
	this->nameLabel->setText(pevi.name.c_str());
	this->descriptionLabel->setText(pevi.description.c_str());
	sprintf(text, "%d", pevi.level);
	this->levelLabel->setText(text);
}

void ProduceElementView::upgradeButton_click(void *sender, ComponentEventArgs &e) {
	Ref<MessageDialog> dialog;
	dialog.initialize(new MessageViewDialog(MessageDialogType_OKCancel));
	dialog->init();
	dialog->setTitle("Are you sure...");
	dialog->setMessage("Are you sure to upgrade this skill?");
	dialog->closed() += event_handler<DialogClosedEventArgs>(
		this, &ProduceElementView::upgradeConfirmDialog_close);
	dialog->show();
}

void ProduceElementView::upgradeConfirmDialog_close(void* sender, DialogClosedEventArgs &e) {
	if (e.getDialogResult() == DialogResult_OK) {
		ProduceUpgradeEventArgs upgradeEventArgs(this, this->getProduceElementViewInfo());
		this->fireManualBubbleEvent(upgradeEventArgs);
		this->setProduceElementViewInfo(upgradeEventArgs.getProduceElementViewInfo());
	}
}